package com.cn.qtms.model;

import com.cn.qtms.entity.vo.sys.User;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
/**
 * @author zhangyang
 */
public class JwtUser implements UserDetails {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

    @JsonSerialize(using = ToStringSerializer.class)
	private Long id;
    private String username;
    private String password;
    private Collection<? extends GrantedAuthority> authorities;
    /**
     * 是否已锁定
     */
    private boolean accountNonLocked;
    /**
     * 是否已过期
     */
    private boolean accountNonExpired;
    /**
     * 是否启用
     */
    private boolean enabled;
    /**
     * 密码是否已过期
     */
    private boolean credentialsNonExpired;
    /**
     * 认证客户端ID
     */
    private String clientId;
    /**
     * 用户类型
     */
    private  String userType;
    /**
     * 供应商id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long supplierId;
    /**
     * 完成步骤
     */
    private Integer step;
    /**
     * 认证中心域,适用于区分多用户源,多认证中心域
     */
    private String domain;

    /**
     * 写一个能直接使用user创建jwtUser的构造器
     * @param user
     * @param roles
     */
    public JwtUser(User user, List<String> roles) {
        id = user.getId();
        username = user.getUsername();
        password = user.getPassword();
        authorities = AuthorityUtils.createAuthorityList(roles.toArray(new String[0]));
        accountNonLocked=!user.getIsLocked();
        userType=user.getUserType();
        accountNonExpired=true;
        credentialsNonExpired=true;
        enabled= "0".equals(user.getStatus())?true:false;
        supplierId=user.getSupplierId();
        clientId=user.getClientId();
        step=user.getStep();
    }

    @Override
    public String toString() {
        return "JwtUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", authorities=" + authorities +
                '}';
    }

}
